﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace Core
{
    public class ProjectHelper
    {
        const string LIST_NAME = "DemoList"; //put your list name here
        const string SITE_URL = "http://demo2010a:4242"; //put your url value here

        public static List<CustomColumnDefinition> InitializeCustomColumnDefinition(List<CustomConfigColumn> columnsToSelect, SPListItemCollection dataSourceCollection)
        {
            List<CustomColumnDefinition> resultList = new List<CustomColumnDefinition>();

            foreach (CustomConfigColumn columnConf in columnsToSelect)
            {
                if (dataSourceCollection.Fields.ContainsField(columnConf.ColumnName))
                {
                    SPField field = dataSourceCollection.Fields.GetFieldByInternalName(columnConf.ColumnName);
                    CustomColumnDefinition column = new CustomColumnDefinition();
                    column.ColumnIdentifier = field.InternalName;
                    column.ColumnDisplayName = field.Title;
                    column.Id = field.Id;
                    
                    resultList.Add(column);
                }
            }
            return resultList;

        }


        public static SPListItemCollection CreateCustomColumnsFromList()
        {
            using (SPSite oSPsite = new SPSite(SITE_URL))
            {
                SPWeb site = oSPsite.RootWeb;
                SPList demoList = site.Lists.TryGetList(LIST_NAME);

                if (demoList == null) throw new NullReferenceException(String.Format("There is no list with name {0}", LIST_NAME));

                return demoList.Items;
            }

        }

        public static void PrintCustomColumnsDefinition(List<CustomColumnDefinition> listToPrint)
        {
            foreach (CustomColumnDefinition column in listToPrint)
            {
                Console.WriteLine("Column attributes:");
                Console.WriteLine(column.ColumnIdentifier);
                Console.WriteLine(column.ColumnDisplayName);
                Console.WriteLine(column.Id);

                Console.WriteLine();
            }
        }
    }
}
